//
//  THMineEarningsViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/1.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMineGroupViewController.h"
#import "THMineGroupTableViewCell.h"
#import "THInviteUserViewController.h"
#import "THMineGourpOneViewController.h"
@interface THMineGroupViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */

@property(nonatomic, strong) NSString                     *count;/**<  值数成员*/
@property(nonatomic, strong) NSString                     *all_count;/**<  所有成员*/
@property(nonatomic, strong) NSString                     *sum;/**< 收益 */
@end

@implementation THMineGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的团队";
    self.count = @"0";
     self.all_count = @"0";
     self.sum = @"0";
    [self stoneobs_ui_configSubView];
    
    __weak typeof(self) weakSelf =  self;
    self.tableView.st_noreslutView = [[STTools_NoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 200, STONEOBS_UI_UISCREEN_WIDTH, 400) title:@"暂无数据" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf send_net_request];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    
    [self stoneobs_fun_st_setRightItemWithTitle:@"邀请" titleColor:STONEOBS_UI_FIRST_TEXT_COLOR];
    // Do any additional setup after loading the view.
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 32, UISCREEN_WIDTH, 20)
                                                            text:@"当前团队人数"
                                                       textColor:WH_UICOLOR_FROM_RGBA(0x333333)
                                                            font:12
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentCenter];
    [header addSubview:stoneobs_ui_label];
    
    
    STLabel * stoneobs_ui_s_label = [[STLabel alloc] initWithFrame:CGRectMake(0, stoneobs_ui_label.bottom+20, UISCREEN_WIDTH, 40)
                                                              text:self.all_count
                                                         textColor:UIColor.blackColor
                                                            font:23
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentCenter];
    [header addSubview:stoneobs_ui_s_label];
    stoneobs_ui_s_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:23];
    
    header.height = stoneobs_ui_s_label.bottom + 20;
    header.backgroundColor = UIColor.whiteColor;
    self.tableView.tableHeaderView = header;
    
    [[self.view viewWithTag:10001] removeFromSuperview];
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_tabBarTop, UISCREEN_WIDTH, 49 + 60)];
    bootomView.backgroundColor = UIColor.whiteColor;
    bootomView.tag = 10001;
    [self.view addSubview:bootomView];
    ({
        STLabel * stoneobs_ui_b_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 49)
                                                                  text:[NSString stringWithFormat:@"总收益:%@",self.sum]
                                                             textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentRight];
        [bootomView addSubview:stoneobs_ui_b_label];
        stoneobs_ui_b_label.right = UISCREEN_WIDTH - 15;
    });
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return THMineGroupTableViewCell.stoneobs_ui_cell_height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 40)];
    CGFloat witdh = UISCREEN_WIDTH / 5;
    CGFloat left = 0;
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, witdh*3-15, 40)
                                                                text:[NSString stringWithFormat:@"直属成员(%@)",self.count]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        left = stoneobs_ui_label.right;
        [view addSubview:stoneobs_ui_label];
    });

    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(left, 0, witdh, 40)
                                                                text:@"加入时间"
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
        left = stoneobs_ui_label.right;
        [view addSubview:stoneobs_ui_label];
    });
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(left, 0, witdh, 40)
                                                                text:@"收益"
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
        left = stoneobs_ui_label.right;
        [view addSubview:stoneobs_ui_label];
    });
    return view;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    THMineGroupTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[THMineGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    THMineGroupItemModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    THMineGroupItemModel * model = self.dataSouce[indexPath.row];
    THMineGourpOneViewController * vc = THMineGourpOneViewController.new;
    vc.model = model;
    [self pushViewController:vc animated:1];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)config_table_footer{
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf send_net_request];
    }];
    
}
- (void)stoneobs_fun_st_rightBarAction:(id)sender{
    [self pushViewController:THInviteUserViewController.new animated:1];
}
- (void)send_net_request{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:@(self.page) forKey:@"page"];
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/user/myTeam"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * dic = [responseObject valueForKey:@"data"];
        NSArray * array = dic[@"items"];
        NSArray * objArray = [THMineGroupItemModel mj_objectArrayWithKeyValuesArray:array];
        NSInteger total = [dic[@"total"] integerValue];//总数量
        
        self.all_count = [dic[@"all_count"] description];
        self.sum = [dic[@"sum"] description];
        self.count = [dic[@"count"] description];
        [self stoneobs_ui_configSubView];
        CGFloat perPage = [dic[@"perPage"] floatValue];
        CGFloat allpage = total / perPage; 
        if (self.page == 1) {
            self.dataSouce = objArray.mutableCopy;
            [self.tableView reloadData];
            if (allpage>1) {
                [self config_table_footer];
            }
            [self.tableView.mj_header endRefreshing];
        }else{
            if (self.page < allpage) {
                [self.tableView.mj_footer endRefreshing];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.dataSouce addObjectsFromArray:objArray];
            [self.tableView reloadData];
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
